package org.geometerplus.fbreader.formats.m17k;

import android.text.TextUtils;


import org.geometerplus.fbreader.book.ReadBook;
import org.geometerplus.fbreader.bookmodel.BookModel;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.fbreader.formats.BookReader;
import org.geometerplus.fbreader.formats.BookReadingException;
import org.geometerplus.fbreader.formats.NativeFormatPlugin;
import org.geometerplus.zlibrary.core.encodings.EncodingCollection;
import org.geometerplus.zlibrary.core.filesystem.ZL17KPlainTxtFile;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.text.view.ZLTextFixedPosition;
import org.geometerplus.zlibrary.text.view.ZLTextPosition;

import java.util.ArrayList;

/**
 * 17k　txt格式小说阅读插件
 * bookId + bookType.equals(".17kTN")或是 bookId.17ktd
 *
 * @author enjoy
 */
public class MTxtNovelPlugin extends NativeFormatPlugin {


    public MTxtNovelPlugin(org.geometerplus.zlibrary.core.util.SystemInfo systemInfo) {
        super(systemInfo, ZL17KPlainTxtFile.ExtentionName);

    }

    @Override
    public String readAnnotation(ZLFile file) {
        return null;
    }

    @Override
    public int priority() {
        return 0;
    }

    @Override
    public EncodingCollection supportedEncodings() {
        return null;
    }

    @Override
    public void readMetainfo(ReadBook book) throws BookReadingException {
        book.setTitle(book.File.getBookName());

    }

    @Override
    public void init(BookReader bookReader, BookModel bookModel) {
        super.init(bookReader, bookModel);
//        MTxtNovelReader txtNovelReader = (MTxtNovelReader) myBookReader;
//        try {
//            txtNovelReader.load();
//        } catch (ErrorMsgException e) {
//            e.printStackTrace();
//        }
//        ((FBReaderApp) FBReaderApp.Instance()).IsAutoPayVIPChapter.setValue(true);
//        final ZL17KPlainTxtFile txt17KFile = (ZL17KPlainTxtFile) txtNovelReader.getStartFile();
//        String lastReadChapterId = txt17KFile.getChapterId();
//        ZLTextPosition lastReadPosition = null;
//
//        if (TextUtils.isEmpty(lastReadChapterId)) {
//            if (txt17KFile.getStartPositioin() != null) {
//                ZLTextPosition mark = txt17KFile.getStartPositioin();
//                lastReadPosition = mark;
//                lastReadChapterId = lastReadPosition.getTag();
//            } else if (TextUtils.isEmpty(lastReadChapterId)) {
//                lastReadPosition = myBookModel.Book.getStoredPosition();
//                if (lastReadPosition != null) {
//                    lastReadChapterId = lastReadPosition.getTag();
//                }
//                txt17KFile.setStartPositioin(lastReadPosition);
//            }
//        }
//        Chapter chapter = txtNovelReader.loadChapterById(lastReadChapterId);
//        if (chapter != null) {
//            chapter = txtNovelReader.gotoChapterById(chapter.getId());
//            txt17KFile.setChapterId(chapter.getId());
//            txt17KFile.setChapterName(chapter.getName());
//            myBookModel.Book.setTitle(chapter.getName());
//
//            if (!Chapter.hasDownload(txtNovelReader.getBookId(), chapter.getId())) {
//                try {
//                    String[] strings = new String[1];
//                    strings[0] = chapter.getId();
//
//                    ArrayList<ChapterContentV4> result = (ArrayList<ChapterContentV4>) ChapterContent4Net.obtain(txtNovelReader.getBookId()).getContentByChapters(strings);
//                    ChapterContentV4 mChapterContentV4 = null;
//                    if(result != null && result.size() > 0){
//                        mChapterContentV4 = result.get(0);
//                    }
//
//                    if (mChapterContentV4 != null) {
//                        if (mChapterContentV4.getContents() == null ) {
//                            if(bookReader instanceof MTxtNovelReader){
//                                ((MTxtNovelReader)bookReader).doAfterAnimation(mChapterContentV4,chapter);
//                            }
//                        }
//                    }
//                } catch (ErrorMsgException e) {
//                    //添加请求失败加载错误页面
//                    ((MTxtNovelReader)bookReader).doAfterAnimation(null,chapter);
//                    e.printStackTrace();
//                }
//
//            } else {
//                if (!Chapter.isInBookPath(txtNovelReader.getBookId(), chapter.getId())) {
//                    FileUtils.copyAssetsData(GlobalApp.getInstance(),
//                            GlobalConstants.ASSET_BOOK_PATH + "/" + txtNovelReader.getBookId(),
//                            GlobalConstants.BOOK_PATH + "/" + txtNovelReader.getBookId(), chapter.getId());
//                }
//            }
//            FileUtils.bumpChapterContent(txt17KFile.getBookId(), chapter.getId());
//            txt17KFile.setChapterId(chapter.getId());
//        }
//
//
//        final FBReaderApp reader = (FBReaderApp) FBReaderApp.Instance();
//        if (reader != null) {
//            reader.BookTextView.loadZLSelectionData(myBookModel.getAllNotes(), myBookModel.getAllLocalUnderLineBlocks());
//        }
//        //Log.d("VVVV", "txt17KFile.isUsingStoredPosition(): " + (txt17KFile.isUsingStoredPosition()));
//        //如果某章节开头阅读，则要保存下当前的阅读进度
//        if (lastReadPosition == null || lastReadPosition.samePositionAs(new ZLTextFixedPosition(0, 0, 0))) {
//            myBookModel.Book.storePosition(reader.BookTextView.getStartCursor());
//        } else {
//            myBookModel.Book.storePosition(lastReadPosition);
//        }
    }




    @Override
    public synchronized void readModel(ZLFile file, BookModel model) throws BookReadingException {
        ZL17KPlainTxtFile zfile = (ZL17KPlainTxtFile) model.Book.File;
        ZL17KPlainTxtFile readFile = (ZL17KPlainTxtFile) file;
        zfile.setChapterId(readFile.getChapterId());
        zfile.setChapterName(readFile.getChapterName());
        zfile.setBookName(readFile.getBookName());
        zfile.setBookId(readFile.getBookId());
        model.Book.setTitle(readFile.getChapterName());
        super.readModel(file, model);
    }
}
